package com.skynet.supervision.utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

public class StringJtUtils {

    /**
     * 按某个指定字段去重
     *
     * @param keyExtractor 需要去重的字段
     * @return java.util.function.Predicate<T>
     * @author joker
     * @date 2023/5/8 13:49
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
}
